/*
Copyright 2008 Ian Thomas

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.wildwinter.anthx;

import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.BuildException;
import java.io.*;

/** haxe2swf task */
public class Haxe2SWF extends Haxe
{
	protected String _out;
	protected Header _header;
	protected String _version;
	protected String _swflib;
	
	private File _outFile;
	
	public Haxe2SWF()
	{
	}
	
    /******************* Attributes ****************************/
    /** Output file */
	public void setOut(String value) 
	{
		_out = value;
		setDependencyTarget(value);
	}
	
	public void setVersion(String value)
	{
		_version=value;
	}
	
	public void setSwflib(String value)
	{
		_swflib=value;
	}
	
	/******************* Elements ****************************/
    public Header createHeader() 
    {
	    if (_header!=null)
	    	throw new BuildException("Can't have more than one header element",getLocation());
        _header = new Header();
        return _header;
    }
	
	/***********************************************************/
    protected void addOptions(ExecTask haxe)
    {
	    if (_out==null)
	    	throw new BuildException("No 'out' attribute set.",getLocation());
	    	
	    _outFile=expandFile(_out);
	    	
	    haxe.createArg().setValue("-swf");
		haxe.createArg().setFile(_outFile);
		
		if (_swflib!=null)
		{
			File file=expandFile(_swflib);
			if (!file.exists())
				throw new BuildException("Can't find swflib:"+file);
			haxe.createArg().setValue("-swf-lib");
			haxe.createArg().setFile(file);
		}
		
		if (_header!=null)
		{
			haxe.createArg().setLine("-swf-header "+_header.format());
		}
		
		if (_version!=null)
		{
			haxe.createArg().setLine("-swf-version "+Integer.parseInt(_version));
		}
    }
    
    protected void postExec(ExecTask haxe)
    {
	    if (!_outFile.exists())
	    	throw new BuildException("Compilation failed:"+_outFile,getLocation());
    }
    
    /***********************************************************/
    public class Header
    {
	    private int _width=0;
	    private int _height=0;
	    private int _frames=0;
	    private int _bg;

        public void setWidth(int value)
        { 
	        _width = value;
	    }
	    
        public void setHeight(int value)
        { 
	        _height = value;
	    }
	    
	    public void setFramerate(int value)
	    {
		    _frames=value;
	    }
	    
	    public void setBackground(String value)
	    {
		    _bg=Integer.parseInt(value, 16);
	    }
	    
	    public String format() throws BuildException
	    {
		    if (_width==0)
		    	throw new BuildException("No width specified.");
		    if (_height==0)
		    	throw new BuildException("No height specified.");
		    if (_frames==0)
		    	throw new BuildException("No framerate specified.");
		    
		    return _width+":"+_height+":"+_frames+":"+Integer.toHexString(_bg);
	    }
    }
}
